﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using SalesManagement.Modules.List;
using FSS.BLL;

namespace SalesManagement.Controls.List
{
    public partial class WareHouseForm : System.Web.UI.UserControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {            
            BranchService branchS = BranchService.GetInstance();
            IList<BranchEntity> lstBranch = branchS.LoadAll();
            if (null != lstBranch)
            {
                this.storeBranch.DataSource = lstBranch;
                this.storeBranch.DataBind();
            }
        }
        public void Show()
        {
            this.winDetails.Show();
        }
        public void Show(string warehouseId)
        {
            WareHouseEntity warehouseE = new WareHouseEntity();
            WareHouseService warehouseS = WareHouseService.GetInstance();
            warehouseE.WarehouseId = int.Parse(warehouseId);
            warehouseE = warehouseS.LoadByPrimaryKey(warehouseE);
            if (null == warehouseE)
                return;
            hdfWarehouseId.Value = warehouseE.WarehouseId;
            txtWarehouseCode.Text = warehouseE.WarehouseCode;
            txtWarehouseName.Text = warehouseE.WarehouseName;
            cbbBranch.SetValue(warehouseE.BranchId);
            txtDescription.Text = warehouseE.Description;
            ckbIsActive.Checked = warehouseE.IsActive;
            this.winDetails.Show();
        }
        public void ShowAndReset()
        {
            this.FormPanel1.Reset();
            this.winDetails.Show();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            WareHouseEntity warehouseE = new WareHouseEntity();
            WareHouseService warehouseS = WareHouseService.GetInstance();
            if ("" != hdfWarehouseId.Value.ToString())
            {
                string warehouseId = hdfWarehouseId.Value.ToString();
                warehouseE.WarehouseId = int.Parse(warehouseId);
                warehouseE = warehouseS.LoadByPrimaryKey(warehouseE);
                warehouseE.WarehouseCode = txtWarehouseCode.Text;
                warehouseE.WarehouseName = txtWarehouseName.Text;
                warehouseE.BranchId = int.Parse(cbbBranch.SelectedItem.Value);
                warehouseE.Description = txtDescription.Text;
                warehouseE.IsActive = ckbIsActive.Checked;
                warehouseE.UpdatedDate = DateTime.Now;
                warehouseE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                warehouseS.Update(warehouseE);
            }
            else
            {
                warehouseE.WarehouseCode = txtWarehouseCode.Text;
                warehouseE.WarehouseName = txtWarehouseName.Text;
                warehouseE.BranchId = int.Parse(cbbBranch.SelectedItem.Value);
                warehouseE.Description = txtDescription.Text;
                warehouseE.IsActive = ckbIsActive.Checked;
                warehouseE.CreatedDate = DateTime.Now;
                warehouseE.CreatedBy = FSSContext.Current.User.EmployeeId;
                warehouseE.UpdatedDate = DateTime.Now;
                warehouseE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                warehouseS.Insert(warehouseE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}